﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LetiLib.Connection;

namespace LedCenter.GUI
{
    public partial class HomePanel : UserControl
    {
        public HomePanel()
        {
            InitializeComponent();
        }

        delegate void SetTextCallback(string stSock, byte[] ba);
        public void SetText(string stSock, byte[] ba)
        {
            if (this.tbLogs.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { stSock, ba });
            }
            else
            {
                string stText = "";
                stText = stSock + " Sends " + ba.Length + " bytes: " + stByteArrayToString(ba);
                tbLogs.SelectionColor = Color.Blue;
                tbLogs.SelectedText = stText + "\n";
                tbLogs.SelectionStart = tbLogs.Text.Length;
                tbLogs.ScrollToCaret();
            }
        }

        delegate void WarningCallback(string stSock, string warning);
        public void Warning(string stSock, string warning)
        {
            if (this.tbLogs.InvokeRequired)
            {
                WarningCallback d = new WarningCallback(Warning);
                this.Invoke(d, new object[] { stSock, warning });
            }
            else
            {
                string stText = "";
                stText = stSock + " Sends Warning: " +warning;
                tbLogs.SelectionColor = Color.Red;
                tbLogs.SelectedText = stText + "\n";
                tbLogs.SelectionStart = tbLogs.Text.Length;
                tbLogs.ScrollToCaret();
            }
        }

        private static string stByteArrayToString(byte[] ba)
        {
            StringBuilder hex = new StringBuilder(ba.Length * 2);
            foreach (byte b in ba)
                hex.AppendFormat("{0,3:x2}", b);
            return hex.ToString();
        }

        delegate void SetInfoCallback(string stSock, string data);
        public void SetInfo(string stSock, string data)
        {
            string stText = "";
            stText = stSock + ": " + data;
            //Logger.Log(stText);
            if (this.tbLogs.InvokeRequired)
            {

                SetInfoCallback d = new SetInfoCallback(SetInfo);
                this.Invoke(d, new object[] { stSock, data });
            }
            else
            {

                tbLogs.SelectionColor = Color.ForestGreen;
                tbLogs.SelectedText = stText + "\n";

                tbLogs.SelectionStart = tbLogs.Text.Length;
                tbLogs.ScrollToCaret();

            }
        }

        delegate void SetTitleCallback(string data);
        public void SetTitle( string data)
        {
            //Logger.Log(stText);
            if (this.tbLogs.InvokeRequired)
            {
                SetTitleCallback d = new SetTitleCallback(SetTitle);
                this.Invoke(d, new object[] { data });
            }
            else
            {
                lbHome.Text = data;
            }
        }
    }
}
